package com.tide.crm.controller;

import java.util.Date;
import java.util.List;

import com.tide.crm.comm.BaseController;
import com.tide.crm.dto.CustomContactStakeholderDto;
import com.tide.crm.dto.LoginUser;
import com.tide.crm.service.CustomContactStakeholderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.tide.crm.page.table.PageTableRequest;
import com.tide.crm.page.table.PageTableHandler;
import com.tide.crm.page.table.PageTableResponse;
import com.tide.crm.page.table.PageTableHandler.CountHandler;
import com.tide.crm.page.table.PageTableHandler.ListHandler;
import com.tide.crm.dao.CustomContactStakeholderDao;
import com.tide.crm.model.CustomContactStakeholder;
import io.swagger.annotations.ApiOperation;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/customContactStakeholder")
public class CustomContactStakeholderController extends BaseController {

    @Autowired
    private CustomContactStakeholderDao customContactStakeholderDao;

    @Autowired
    private CustomContactStakeholderService customContactStakeholderService;

    @PostMapping("saveInfo")
    @ApiOperation(value = "保存")
    public String  save(@RequestBody CustomContactStakeholderDto customContactStakeholderDto,String[] eventArray,String[] eventDateArray, HttpServletRequest request) {
        LoginUser user = super.getUserInfoByToken(request);
        Date date = new Date();
        customContactStakeholderDto.setCreater(user.getUsername());
        customContactStakeholderDto.setModifyer(user.getUsername());
        customContactStakeholderDto.setCreateDate(date);
        customContactStakeholderDto.setModifyDate(date);
        customContactStakeholderService.save(customContactStakeholderDto,eventArray,eventDateArray);
        return "success";
    }

    @GetMapping("getInfo/{id}")
    @ApiOperation(value = "根据id获取")
    public CustomContactStakeholderDto get(@PathVariable Long id) {
        return customContactStakeholderService.getById(id);
    }

    @PostMapping("editInfo")
    @ApiOperation(value = "修改")
    public String update(@RequestBody CustomContactStakeholderDto customContactStakeholderDto,String[] eventArray,String[] eventDateArray, HttpServletRequest request) {
        LoginUser user = super.getUserInfoByToken(request);
        Date date = new Date();
        customContactStakeholderDto.setModifyer(user.getUsername());
        customContactStakeholderDto.setModifyDate(date);
        customContactStakeholderService.update(customContactStakeholderDto, eventArray, eventDateArray);
        return "success";
    }

    @GetMapping("getInfoList")
    @ApiOperation(value = "列表")
    public PageTableResponse list(PageTableRequest request) {
        return new PageTableHandler(new CountHandler() {
            @Override
            public int count(PageTableRequest request) {
                return customContactStakeholderService.count(request.getParams());
            }
        }, new ListHandler() {
            @Override
            public List<CustomContactStakeholderDto> list(PageTableRequest request) {
                return customContactStakeholderService.list(request.getParams(), request.getOffset(), request.getLimit());
            }
        }).handle(request);
    }

    @GetMapping("delInfoById/{id}")
    @ApiOperation(value = "删除")
    public void delete(@PathVariable Long id) {
        customContactStakeholderService.delete(id);
    }
}
